import Vue from 'vue';

import iView from 'iview';
Vue.use(iView);
import VueBus from 'vue-bus';
Vue.use(VueBus);

import 'iview/dist/styles/iview.css';
import "./styles/main.less";
import "./styles/main.css";
import {createApp} from "./app";

var {app,router,store} =createApp({mode:'hash'});
// 这里假定 App.vue 模板中根元素具有 `id="app"`
app.$options.asyncData({
  store,
  route:router.currentRoute
}).then((e)=>{
  //console.log(e);
  //app.$mount('#app')
  mount();
}).catch(()=>{
  //app.$mount('#app')
  mount();
})

function mount(){
  app.$mount('#app')
  router.beforeResolve((to, from, next) => {
    if (to.matched.some(record => record.meta.requiresAuth)) {
      if (!store.getters.getToken) {
          //iview.Message.info('请登录！');
          return next({
              path: '/login',
              query: { redirect: to.fullPath }
          })
      }
    }
    
    const matched = router.getMatchedComponents(to)
    const prevMatched = router.getMatchedComponents(from)
    let diffed = false
    const activated = matched.filter((c, i) => {
      return diffed || (diffed = (prevMatched[i] !== c))
    })
    const asyncDataHooks = activated.map(c => c.asyncData).filter(_ => _);
    
    if (!asyncDataHooks.length) {
      return next()
    }
  
    //bar.start()
    Promise.all(asyncDataHooks.map(hook => hook({ store, route: to })))
    .then(() => {
      //bar.finish()
      next()
    })
    .catch(next)
  })
}

  
